package we3d.material 
{
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import we3d.mesh.Face;
	
	public class BitmapAttributes extends FlatAttributes implements ISurfaceAttributes
	{
		public function BitmapAttributes (bitmap:BitmapData=null) {
			texture = bitmap;
		}
		
		public var _texture:BitmapData;
		public var _w:Number;
		public var _h:Number;
		public var smooth:Boolean=false;
		public var repeat:Boolean=false;
		
		/**
		* the bitmapdata of the texture
		* @return
		*/
		public function get texture () :BitmapData { return _texture; }
		public function set texture (b:BitmapData) :void {
			if(b is BitmapData) {
				_texture = b;
				_w = b.width;
				_h = b.height;
			}else{
				_texture = null;
				_w = 0;
				_h = 0;
			}
		}
		
		/**
		* Pre set the width and height of the bitmap to avoid problems with precalculated properties in polygons.
		* When you change the texture of a surface wich is already assigned to polygons, you have to call the SceneObject.initTextureSizeOnPolygons method to precalculate the new texture matrix in all polygons again. <br/>
		* This have to be done only if the bitmap size changes <br/>
		* @param	w
		* @param	h
		*/
		public function setSize (w:Number, h:Number) :void {
			_w = w;
			_h = h;
		}
		/**
		* @private
		*/
		public override function initFace (f:Face) :void {
			if(f.uvs) {
				var auv:Array = f.uvs;	
				if(auv.length > 2) {
					f.u1 = auv[0].u * _w;
					f.v1 = auv[0].v * _h;
					f.u2 = auv[1].u * _w;
					f.v2 = auv[1].v * _h;
					f.u3 = auv[2].u * _w;
					f.v3 = auv[2].v * _h;
					f.tMat = new Matrix((f.u2 - f.u1)/_w, (f.v2 - f.v1)/_w, (f.u3 - f.u1)/_h, (f.v3 - f.v1)/_h, f.u1, f.v1);
					f.tMat.invert();
				}
			}
		}
		
	}
}